package com.ljn.service.imp;

import com.ljn.core.dto.Teacher;
import com.ljn.core.dto.common.exceptions.UserIsExistedException;
import com.ljn.core.dto.common.utils.UUIDUtils;
import com.ljn.core.dto.common.utils.ency.MD5EncryptUtil;
import com.ljn.mapper.TeacherMapper;
import com.ljn.service.TeacherService;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by liujunnan on 2017/11/3.
 */
@Named
public class TeacherServiceImpl implements TeacherService {
    @Inject
    TeacherMapper teacherMapper;

    private Teacher findByUsername(String userName) {
        return teacherMapper.findUserByUsername(userName);
    }

    @Transactional
    public Long register(Teacher teacher) {
        Teacher result = this.findByUsername(teacher.getUserName());
        if (result != null) {
            throw new UserIsExistedException("用户存在");
        } else {
            teacher.setUserId(UUIDUtils.get32UUIDAsLowerCase());
            teacher.setPassword(MD5EncryptUtil.encryptPassword(teacher.getPassword(), teacher.getUserId()));
            teacherMapper.insertUser(teacher);
            teacherMapper.insertTeacher(teacher);
            return teacher.getPhone();
        }
    }

    public Teacher selectTeacher(String userId) {
        Teacher teacher = teacherMapper.findTeacherById(userId);
        return teacher;
    }

    @Override
    public boolean changeUserInfo(Teacher teacher) {

        boolean i = this.teacherMapper.updateByPrimaryKeySelective(teacher) > 0 ? true:false;
        boolean t = this.teacherMapper.updateByPrimaryKeySelectiveUser(teacher) >0 ? true:false;
        return  i&&t ;
    }
}
